//
//  XCSingerButtonViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/11.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCSingerButtonViewController.h"
#import "XCUrlManger.h"
#import "XCSingerButtonCollectionViewCell.h"
#import "XCAFNetTool.h"
#import "XCSingerDataModel.h"
#import "XCInTheSingerViewController.h"
@interface XCSingerButtonViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataSource;

@end

@implementation XCSingerButtonViewController
- (void)dealloc {
    _collectionView.dataSource = nil;
    _collectionView.delegate = nil;
    
    [_collectionView release];
    [_dataSource release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"歌手";
    [self NetRequest];
    [self collectionBuildAction];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
}
#pragma mark - 网络请求
- (void)NetRequest {
    [XCAFNetTool GETNetWithUrl:[XCUrlManger SingerNetRequest] body:nil headerFile:nil response:XCJSON success:^(id result) {

        self.dataSource = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in result[@"data"]) {
            XCSingerDataModel *singerDataModel = [[XCSingerDataModel alloc] initWithDataSource:dic];
            [self.dataSource addObject:singerDataModel];
            [singerDataModel release];
        }
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"网络请求失败");
    }];
}
#pragma mark - collection 的创建
- (void)collectionBuildAction {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake(wide / 3 - 2, wide / 3 + 25);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 1, 100, 1);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
//    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView registerClass:[XCSingerButtonCollectionViewCell class] forCellWithReuseIdentifier:@"XCSingerButtonCollectionViewCellIdentifier"];
    
    [self.view addSubview:self.collectionView];
    [_collectionView release];
}
#pragma mark - collection Delegate 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        XCSingerButtonCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"XCSingerButtonCollectionViewCellIdentifier" forIndexPath:indexPath];
        cell.singerDataModel = self.dataSource[indexPath.row];
        return cell;
    
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XCSingerDataModel *singerDataModel = self.dataSource[indexPath.row];
    
    XCInTheSingerViewController *inTheSingerVC = [[XCInTheSingerViewController alloc] initWithId:singerDataModel.singerListId WithTitle:singerDataModel.title];
//    NSLog(@"%@", singerDataModel.singerListId);
    [self.navigationController pushViewController:inTheSingerVC animated:YES];
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
